<template>

        <el-form ref="form" label-width="70px" style="padding: 10px;box-sizing: border-box;height: 100%">
                    <el-form-item label="员工姓名" class="mt">
                        <el-input size="mini" v-model="user.name" placeholder="请输入真实姓名"></el-input>
                    </el-form-item>
                    <el-form-item label="员工性别">
                        <el-radio-group v-model="user.sex">
                            <el-radio label="1">男</el-radio>
                            <el-radio label="0">女</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="员工帐号">
                        <el-input size="mini" v-model="user.username" placeholder="请输入前台账号(手机号)"></el-input>
                    </el-form-item>
                    <el-form-item label="所属部门">
                        <el-input size="mini" v-model="bname" placeholder="请选择所属部门" disabled>
                            <el-button slot="append" icon="el-icon-search" @click="addBumen"></el-button>
                        </el-input>
                    </el-form-item>
                    <el-form-item label="所属职位">
                        <el-input size="mini" v-model="pname" placeholder="请选择所属职位" disabled>
                            <el-button slot="append" icon="el-icon-search" @click="selzhi"></el-button>
                        </el-input>
                    </el-form-item>
                    <el-form-item label="所属职称">
                        <el-select size="mini" v-model="user.zhichengid" class="width" placeholder="请选择所属职称">
                            <el-option v-for="(z,index) in zlist" :label="z.name" :value="z.id" :key="index">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="员工描述">
                        <el-input type="textarea" size="mini" v-model="user.remarks" placeholder="请输入员工描述"></el-input>
                    </el-form-item>
                    <el-form-item label="管理层">
                        <el-switch v-model="user.ismanage" on-text="是" off-text="否">
                        </el-switch>
                    </el-form-item>
                    <el-form-item label="">
                        <el-button type="success" size="mini" @click="submit">保存</el-button>
                    </el-form-item>
                </el-form>
</template>
<script>
import C from '@/common/common.js'

export default {
  name: 'addUser',
  data () {
    return {
      user: {
        id: 0,
        name: '',
        sex: '0',
        username: '',
        positionid: '',
        zhichengid: '',
        departmentid: '',
        ismanage: false,
        remarks: ''
      },
      pname: '',
      bname: '',
      zlist: []
    }
  },
  methods: {
    selzhi: async function () {
      let res = await C.openBrowserWindow('/addPosition', {}, { width: 400, height: 600, useSpa: false, spaModel: 'organization', transparent: true, frame: false })
      if (res) {
        this.user.positionid = res.id
        this.pname = res.name
        let data = await C.ajax('/organization/public/getplist', { id: res.id }, 'post')
        if (data) this.zlist = data
        this.user.zhichengid = ''
      }
    },
    submit: async function (index) {
      if (!(/^[\u4e00-\u9fa5]{2,4}$/.test(this.user.name))) {
        C.error('姓名格式不正确！')
        return false
      } else if (!(/^1[345678]\d{9}$/.test(this.user.username))) {
        C.error('手机格式不正确！')
        return false
      } else if (this.user.positionid == '') {
        C.error('请选择职位！')
        return false
      } else if (this.user.zhichengid == '') {
        C.error('请选择职称！')
        return false
      }
      this.user.ismanage = this.user.ismanage ? 1 : 0
      var aaa = false
      this.zlist.forEach((res) => {
        if (res.id === this.user.zhichengid) {
          aaa = true
          return aaa
        }
      })
      if (aaa) {
        let data = await C.ajax('/organization/user/addedit', this.user, 'post')
        if (data) {
          C.closeBrowserWindow(data)
        }
      } else {
        C.error('请选择对应职位的职称！')
        return false
      }
    },
    async addBumen () {
      let bumenInfo = await C.openBrowserWindow('/selectDepartment2', {}, {
        width: 360,
        height: 500,
        useSpa: false,
        spaModel: 'organization',
        transparent: true,
        frame: false
      })
      this.bname = bumenInfo.name
      this.user.departmentid = bumenInfo.id
    }
  },
  mounted: async function () {
    let row = C.getDatas().row
    console.log(row)
    if (row) {
      this.user.id = row.id
      this.user.name = row.name
      this.user.sex = row.sex.toString()
      this.user.username = row.username
      this.user.positionid = row.positionid
      this.user.zhichengid = row.zhichengid
      this.user.ismanage = !!row.ismanage
      this.user.remarks = row.remarks
      this.user.departmentid = row.departmentid
      this.pname = row.pname
      this.bname = row.bumenname
      let data = await C.ajax('/organization/public/getplist', { id: row.positionid }, 'post')
      if (data) this.zlist = data
    }
  }
}
</script>